<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/2
 * Time: 0:52
 */

namespace app\common\model;


use think\Db;

class Comment extends Base
{

    /**
     * 通过条件获取评论的数量 V 1.0
     * @param array $param
     * @return int|string
     */
    public function getNormalCommentsCountByCondition($param = []){
        $count = Db::table('ent_comment')
            ->alias(['ent_comment' => 'a', 'ent_user' => 'b'])
            ->join('ent_user', 'a.user_id = b.id AND a.news = ' . $param['news_id'])
            ->count();

        return $count;
    }

    /**
     * 通过条件获取列表数据 V 1.0
     * @param array $param
     * @param int $from
     * @param int $size
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getNormalCommentsByCondition($param = [], $from = 0, $size = 5){
        $result = Db::table('ent_comment')
            ->alias(['ent_comment' => 'a', 'ent_user' => 'b'])
            ->join('ent_user', 'a.user_id = b.id AND a.news = ' . $param['news_id'])
            ->limit($from, $size)
            ->order(['a.id' => 'desc'])
            ->select();

        return $result;
    }

    /**
     * V 2.0
     * @param array $param
     * @return int|string
     */
    public function getCountByCondition($param = []){
        return $this->where($param)->field('id')->count();
    }

    /**
     * V 2.0
     * @param array $param
     * @param int $from
     * @param int $size
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getListsByCondition($param = [], $from = 0, $size = 5){
        return $this->where($param)->field('*')->limit($from, $size)->order(['id' => 'desc'])->select();
    }

}